package com.example.shiwuyouproject.ui.fragment.home.fragment

import android.os.Bundle
import android.view.LayoutInflater
import androidx.lifecycle.Observer
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseVmFragment
import com.example.shiwuyouproject.databinding.FragmentLatestrecruitmentBinding
import com.example.shiwuyouproject.ui.fragment.home.activity.CourseDetailsActivity
import com.example.shiwuyouproject.ui.fragment.home.viewmodel.LatestRecruitmentFragmentViewModel
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.MyApplicationAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.bean.MyApplicationBean
import com.example.shiwuyouproject.utils.ActStartUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 招聘数据列表页
 */
class LatestRecruitmentFragment :
    BaseVmFragment<FragmentLatestrecruitmentBinding, LatestRecruitmentFragmentViewModel>() {
    companion object {
        fun newInstance(type: String?): LatestRecruitmentFragment {
            val newFragment = LatestRecruitmentFragment()
            val bundle = Bundle()
            bundle.putString("type", type)
            newFragment.arguments = bundle
            return newFragment
        }
    }

    override fun viewModelClass(): Class<LatestRecruitmentFragmentViewModel> =
        LatestRecruitmentFragmentViewModel::class.java

    override fun getViewBinding(): FragmentLatestrecruitmentBinding {
        return FragmentLatestrecruitmentBinding.inflate(layoutInflater)
    }

    private var type = ""
    override fun initView() {
        type = arguments?.getString("type").toString()


    }

    private var myApplicationAdapter: MyApplicationAdapter? = null
    override fun initData() {
        myApplicationAdapter = MyApplicationAdapter()
        mBinding.myRecyclerView.adapter = myApplicationAdapter
        val mview = LayoutInflater.from(context).inflate(R.layout.view_nodata, null)
        myApplicationAdapter?.emptyView = mview
        mBinding.mySmartRefreshLayout.autoRefresh()
    }

    private var pageNumber = 1
    override fun setListener() {
        mBinding.mySmartRefreshLayout.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNumber = 1
                when (type) {
                    "0" -> {//推荐
                        mViewModel.getPostRecommendLists(pageNumber.toString())
                    }
                    "1" -> {//最新
                        mViewModel.getPostLatestLists(pageNumber.toString())
                    }
                }

            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNumber++
                when (type) {
                    "0" -> {//推荐
                        mViewModel.getPostRecommendLists(pageNumber.toString())
                    }
                    "1" -> {//最新
                        mViewModel.getPostLatestLists(pageNumber.toString())
                    }
                }
            }
        })
        myApplicationAdapter?.setOnItemClickListener { adapter, view, position ->
            val item: MyApplicationBean? = myApplicationAdapter?.getItem(position)
            val mBundle = Bundle()
            mBundle.putString("post_id", item?.id)
            ActStartUtils.startAct(context, CourseDetailsActivity::class.java, mBundle)
        }
    }

    override fun observe() {
        mViewModel.getPostRecommendList.observe(this,
            Observer<MutableList<MyApplicationBean>> {
                if (pageNumber == 1) {
                    myApplicationAdapter?.setNewData(it)
                    mBinding.mySmartRefreshLayout.finishRefresh()
                } else {
                    myApplicationAdapter?.addData(it)
                    mBinding.mySmartRefreshLayout.finishLoadMore()
                }
                if (pageNumber > 1 && it.size == 0) {
                    mBinding.mySmartRefreshLayout.finishLoadMoreWithNoMoreData()
                }
            })
    }

}